import time

from selenium.webdriver.support.wait import WebDriverWait
from appium.webdriver.common.appiumby import By

from appPage.enter_homepage import EnterHomePage
from appPage.login import Login
from config import *
from utils.driver_utils import DriverUtils


class AddDefaultAddress:
    def add_default_address(self, driver, name, phone, address):
        # 滑动页面,向上滑动
        driver.swipe(start_x=540, start_y=1600, end_x=500, end_y=400)
        # # 注意这里的滑动是往下滑动从element1滑倒element2,这里有问题,要修改后才能用
        # element1 = WebDriverWait(driver, 10).until(lambda x: x.find_element(by=By.ID, value="com.tpshop.malls:id/nick_name_tv"))
        # element2 = WebDriverWait(driver, 10).until(lambda x: x.find_element(by=By.ID, value="com.tpshop.malls:id/shortcut_iv"))
        # driver.scroll(element1,element2)

        WebDriverWait(driver, 10).until(lambda x: x.find_element(by=By.XPATH, value="//*[@text='地址管理']")).click()
        WebDriverWait(driver, 10).until(lambda x: x.find_element(by=By.ID, value="com.tpshop.malls:id/add_address_tv")).click()
        WebDriverWait(driver, 10).until(lambda x: x.find_element(by=By.ID, value="com.tpshop.malls:id/consignee_name_et")).send_keys(name)
        WebDriverWait(driver, 10).until(lambda x: x.find_element(by=By.ID, value="com.tpshop.malls:id/consignee_mobile_et")).send_keys(phone)
        WebDriverWait(driver, 10).until(lambda x: x.find_element(by=By.ID, value="com.tpshop.malls:id/consignee_region_tv")).click()
        WebDriverWait(driver, 10).until(lambda x: x.find_element(by=By.XPATH, value="//*[@text='北京市']")).click()
        WebDriverWait(driver, 10).until(lambda x: x.find_element(by=By.XPATH, value="//*[@text='市辖区']")).click()
        WebDriverWait(driver, 10).until(lambda x: x.find_element(by=By.XPATH, value="//*[@text='东城区']")).click()
        WebDriverWait(driver, 10).until(lambda x: x.find_element(by=By.XPATH, value="//*[@text='东华门街道']")).click()
        time.sleep(2)
        driver.find_element(by=By.ID, value="com.tpshop.malls:id/btn_right").click()
        WebDriverWait(driver, 10).until(lambda x: x.find_element(by=By.ID, value="com.tpshop.malls:id/consignee_address_et")).send_keys(address)
        WebDriverWait(driver, 10).until(lambda x: x.find_element(by=By.ID, value="com.tpshop.malls:id/set_default_sth")).click()
        WebDriverWait(driver, 10).until(lambda x: x.find_element(by=By.ID, value="com.tpshop.malls:id/save_tv")).click()
        WebDriverWait(driver, 10).until(lambda x: x.find_element(by=By.ID, value="com.tpshop.malls:id/title_back_img")).click()

if __name__ == '__main__':
    # 驱动
    driver_utils = DriverUtils()
    driver = driver_utils.get_driver()
    # 进入页面
    enter = EnterHomePage()
    # 登录
    login = Login()
    # 添加地址
    add = AddDefaultAddress()

    enter.enter_homepage(driver)
    login.login(driver, PHONE, PWD)
    add.add_default_address(driver, NAME, PHONE, ADDRESS)
    time.sleep(5)

    driver_utils.quit_driver()


